/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * mdijSimProd.java
 *
 * Created on 18-feb-2009, 18:38:41
 */
package gui.forms;

import gui.tools.Receiver;
import gui.tools.XMLFilter;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import lib.model.FactoryModel;
import lib.model.Interval;
import lib.plan.ProductionPlan;
import lib.simul.Simulation;

/**
 *
 * @author jorge
 */
public class FrmMain extends javax.swing.JFrame implements Receiver {

    private FactoryModel factory;
    private ProductionPlan plan;
    private Simulation simulation;
    private File fmFile;
    private File ppFile;

    /** Creates new form mdijSimProd */
    public FrmMain() {
        initComponents();

        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        factory = null;
        plan = null;
        simulation = null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mnuMain = new javax.swing.JMenuBar();
        mnuFile = new javax.swing.JMenu();
        mnuFactoryModel = new javax.swing.JMenu();
        mnuFM = new javax.swing.JMenuItem();
        sepFM = new javax.swing.JSeparator();
        mnuFMNew = new javax.swing.JMenuItem();
        mnuFMOpen = new javax.swing.JMenuItem();
        mnuFMSave = new javax.swing.JMenuItem();
        mnuFMSaveAs = new javax.swing.JMenuItem();
        sepFM1 = new javax.swing.JSeparator();
        mnuFMEdit = new javax.swing.JMenuItem();
        mnuProductionPlan = new javax.swing.JMenu();
        mnuPP = new javax.swing.JMenuItem();
        sepPP = new javax.swing.JSeparator();
        mnuPPNew = new javax.swing.JMenuItem();
        mnuPPOpen = new javax.swing.JMenuItem();
        mnuPPSave = new javax.swing.JMenuItem();
        mnuPPSaveAs = new javax.swing.JMenuItem();
        sepPP1 = new javax.swing.JSeparator();
        mnuPPEdit = new javax.swing.JMenuItem();
        sepFile = new javax.swing.JSeparator();
        mnuQuit = new javax.swing.JMenuItem();
        mnuSimulation = new javax.swing.JMenu();
        mnuPlay = new javax.swing.JMenuItem();
        mnuPause = new javax.swing.JMenuItem();
        mnuStop = new javax.swing.JMenuItem();
        sepSimulation = new javax.swing.JSeparator();
        mnuResults = new javax.swing.JMenuItem();
        mnuHelp = new javax.swing.JMenu();
        mnuAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jSimProd");
        setName("FrmMain"); // NOI18N
        setResizable(false);

        mnuFile.setMnemonic('F');
        mnuFile.setText("File");

        mnuFactoryModel.setMnemonic('M');
        mnuFactoryModel.setText("Factory model");

        mnuFM.setText("Factory model not set");
        mnuFM.setEnabled(false);
        mnuFM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuFMActionPerformed(evt);
            }
        });
        mnuFactoryModel.add(mnuFM);
        mnuFactoryModel.add(sepFM);

        mnuFMNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnuFMNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/new.png"))); // NOI18N
        mnuFMNew.setMnemonic('N');
        mnuFMNew.setText("New factory model");
        mnuFMNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuFMNewActionPerformed(evt);
            }
        });
        mnuFactoryModel.add(mnuFMNew);

        mnuFMOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        mnuFMOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/open.png"))); // NOI18N
        mnuFMOpen.setMnemonic('O');
        mnuFMOpen.setText("Open factory model...");
        mnuFMOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuFMOpenActionPerformed(evt);
            }
        });
        mnuFactoryModel.add(mnuFMOpen);

        mnuFMSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mnuFMSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/save.png"))); // NOI18N
        mnuFMSave.setMnemonic('S');
        mnuFMSave.setText("Save factory model");
        mnuFMSave.setEnabled(false);
        mnuFMSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuFMSaveActionPerformed(evt);
            }
        });
        mnuFactoryModel.add(mnuFMSave);

        mnuFMSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mnuFMSaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/save-as.png"))); // NOI18N
        mnuFMSaveAs.setMnemonic('a');
        mnuFMSaveAs.setText("Save factory model as...");
        mnuFMSaveAs.setEnabled(false);
        mnuFMSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuFMSaveAsActionPerformed(evt);
            }
        });
        mnuFactoryModel.add(mnuFMSaveAs);
        mnuFactoryModel.add(sepFM1);

        mnuFMEdit.setText("Edit factory model...");
        mnuFMEdit.setEnabled(false);
        mnuFMEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuFMEditActionPerformed(evt);
            }
        });
        mnuFactoryModel.add(mnuFMEdit);

        mnuFile.add(mnuFactoryModel);

        mnuProductionPlan.setMnemonic('P');
        mnuProductionPlan.setText("Production plan");
        mnuProductionPlan.setEnabled(false);

        mnuPP.setText("Production plan not set");
        mnuPP.setEnabled(false);
        mnuPP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPPActionPerformed(evt);
            }
        });
        mnuProductionPlan.add(mnuPP);
        mnuProductionPlan.add(sepPP);

        mnuPPNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J, java.awt.event.InputEvent.CTRL_MASK));
        mnuPPNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/new.png"))); // NOI18N
        mnuPPNew.setMnemonic('N');
        mnuPPNew.setText("New production plan");
        mnuPPNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPPNewActionPerformed(evt);
            }
        });
        mnuProductionPlan.add(mnuPPNew);

        mnuPPOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_K, java.awt.event.InputEvent.CTRL_MASK));
        mnuPPOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/open.png"))); // NOI18N
        mnuPPOpen.setMnemonic('O');
        mnuPPOpen.setText("Open production plan...");
        mnuPPOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPPOpenActionPerformed(evt);
            }
        });
        mnuProductionPlan.add(mnuPPOpen);

        mnuPPSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        mnuPPSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/save.png"))); // NOI18N
        mnuPPSave.setMnemonic('S');
        mnuPPSave.setText("Save production plan");
        mnuPPSave.setEnabled(false);
        mnuPPSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPPSaveActionPerformed(evt);
            }
        });
        mnuProductionPlan.add(mnuPPSave);

        mnuPPSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mnuPPSaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/save-as.png"))); // NOI18N
        mnuPPSaveAs.setMnemonic('a');
        mnuPPSaveAs.setText("Save production plan as...");
        mnuPPSaveAs.setEnabled(false);
        mnuPPSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPPSaveAsActionPerformed(evt);
            }
        });
        mnuProductionPlan.add(mnuPPSaveAs);
        mnuProductionPlan.add(sepPP1);

        mnuPPEdit.setText("Edit production plan");
        mnuPPEdit.setEnabled(false);
        mnuPPEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPPEditActionPerformed(evt);
            }
        });
        mnuProductionPlan.add(mnuPPEdit);

        mnuFile.add(mnuProductionPlan);
        mnuFile.add(sepFile);

        mnuQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        mnuQuit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/quit.png"))); // NOI18N
        mnuQuit.setMnemonic('Q');
        mnuQuit.setText("Quit");
        mnuQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuQuitActionPerformed(evt);
            }
        });
        mnuFile.add(mnuQuit);

        mnuMain.add(mnuFile);

        mnuSimulation.setMnemonic('S');
        mnuSimulation.setText("Simulation");
        mnuSimulation.setToolTipText("");

        mnuPlay.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        mnuPlay.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/play.png"))); // NOI18N
        mnuPlay.setMnemonic('S');
        mnuPlay.setText("Start simulation");
        mnuPlay.setEnabled(false);
        mnuPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPlayActionPerformed(evt);
            }
        });
        mnuSimulation.add(mnuPlay);

        mnuPause.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F7, 0));
        mnuPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/pause.png"))); // NOI18N
        mnuPause.setMnemonic('P');
        mnuPause.setText("Pause");
        mnuPause.setEnabled(false);
        mnuPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPauseActionPerformed(evt);
            }
        });
        mnuSimulation.add(mnuPause);

        mnuStop.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F8, 0));
        mnuStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/stop.png"))); // NOI18N
        mnuStop.setMnemonic('t');
        mnuStop.setText("Stop");
        mnuStop.setEnabled(false);
        mnuStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuStopActionPerformed(evt);
            }
        });
        mnuSimulation.add(mnuStop);
        mnuSimulation.add(sepSimulation);

        mnuResults.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F10, 0));
        mnuResults.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/results.png"))); // NOI18N
        mnuResults.setMnemonic('R');
        mnuResults.setText("Results");
        mnuResults.setEnabled(false);
        mnuResults.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuResultsActionPerformed(evt);
            }
        });
        mnuSimulation.add(mnuResults);

        mnuMain.add(mnuSimulation);

        mnuHelp.setMnemonic('H');
        mnuHelp.setText("Help");

        mnuAbout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, java.awt.event.InputEvent.CTRL_MASK));
        mnuAbout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/about.png"))); // NOI18N
        mnuAbout.setMnemonic('A');
        mnuAbout.setText("About jSimProd...");
        mnuAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAboutActionPerformed(evt);
            }
        });
        mnuHelp.add(mnuAbout);

        mnuMain.add(mnuHelp);

        setJMenuBar(mnuMain);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 173, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 89, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-177)/2, (screenSize.height-137)/2, 177, 137);
    }// </editor-fold>//GEN-END:initComponents

    private void mnuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAboutActionPerformed
        new FrmAbout().setVisible(true);
    }//GEN-LAST:event_mnuAboutActionPerformed

    private void mnuFMNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFMNewActionPerformed
        new FrmFactoryModel(null, this).setVisible(true);
        mnuFMEdit.setEnabled(false);
        mnuFMSaveAs.setEnabled(true);
    }//GEN-LAST:event_mnuFMNewActionPerformed

    private void mnuFMOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFMOpenActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.setFileFilter(new XMLFilter());
        int selection = jfc.showOpenDialog(this);
        if (selection == JFileChooser.APPROVE_OPTION) {
            fmFile = jfc.getSelectedFile();
            try {
                factory = FactoryModel.Load(fmFile);
                mnuProductionPlan.setEnabled(true);
                mnuFM.setText("Factory model: " + factory.getName());
                mnuFMEdit.setEnabled(true);
                mnuFMSave.setEnabled(true);
                mnuFMSaveAs.setEnabled(true);
                new FrmFactoryModel(factory, this).setVisible(true);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane,
                        "The file specified doesn't contain a factory model.",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

        }
    }//GEN-LAST:event_mnuFMOpenActionPerformed

    private void mnuQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuQuitActionPerformed
        this.setVisible(false);
        System.exit(0);
    }//GEN-LAST:event_mnuQuitActionPerformed

    private void mnuPPOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPPOpenActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.setFileFilter(new XMLFilter());
        int selection = jfc.showOpenDialog(this);
        if (selection == JFileChooser.APPROVE_OPTION) {
            ppFile = jfc.getSelectedFile();
            try {
                this.plan = ProductionPlan.Load(ppFile);
                mnuPP.setText("Production plan: " + plan.getName());
                mnuPPEdit.setEnabled(true);
                mnuPPSave.setEnabled(true);
                mnuPPSaveAs.setEnabled(true);
                mnuPlay.setEnabled(true);
                new FrmProductionPlan(plan, factory, this).setVisible(true);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane,
                        "The file specified doesn't contain a production plan.",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
}//GEN-LAST:event_mnuPPOpenActionPerformed

    private void mnuPPNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPPNewActionPerformed
        new FrmProductionPlan(null, factory, this).setVisible(true);
        mnuPPSave.setEnabled(false);
        mnuPPEdit.setEnabled(false);
        mnuPPSaveAs.setEnabled(true);
        mnuPlay.setEnabled(true);
}//GEN-LAST:event_mnuPPNewActionPerformed

    private void mnuPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPlayActionPerformed
        new FrmSimulation(this).setVisible(true);
        this.mnuPause.setEnabled(true);
        this.mnuStop.setEnabled(true);
        this.mnuResults.setEnabled(true);
    }//GEN-LAST:event_mnuPlayActionPerformed

    private void mnuFMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFMActionPerformed
    }//GEN-LAST:event_mnuFMActionPerformed

    private void mnuPPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPPActionPerformed
    }//GEN-LAST:event_mnuPPActionPerformed

    private void mnuFMEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFMEditActionPerformed
        new FrmFactoryModel(factory, this).setVisible(true);
    }//GEN-LAST:event_mnuFMEditActionPerformed

    private void mnuPPEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPPEditActionPerformed
        new FrmProductionPlan(plan, factory, this).setVisible(true);
    }//GEN-LAST:event_mnuPPEditActionPerformed

    private void mnuFMSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFMSaveActionPerformed
        try {
            factory.SaveAs(fmFile);
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(rootPane,
                    "Unable to save the factory model.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_mnuFMSaveActionPerformed

    private void mnuPPSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPPSaveActionPerformed
        try {
            plan.SaveAs(ppFile);
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(rootPane,
                    "Unable to save the production plan.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_mnuPPSaveActionPerformed

    private void mnuFMSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFMSaveAsActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.setFileFilter(new XMLFilter());
        int selection = jfc.showSaveDialog(jfc);
        if (selection == JFileChooser.APPROVE_OPTION) {
            fmFile = jfc.getSelectedFile();
            try {
                factory.SaveAs(fmFile);
                mnuFMSave.setEnabled(true);
                mnuFMEdit.setEnabled(true);
                mnuFM.setText("Factory model: " + factory.getName());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane,
                        e.getMessage(),
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
    }//GEN-LAST:event_mnuFMSaveAsActionPerformed

    private void mnuPPSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPPSaveAsActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setAcceptAllFileFilterUsed(false);
        jfc.setFileFilter(new XMLFilter());
        int selection = jfc.showSaveDialog(this);
        if (selection == JFileChooser.APPROVE_OPTION) {
            ppFile = jfc.getSelectedFile();
            try {
                plan.SaveAs(ppFile);
                mnuPPSave.setEnabled(true);
                mnuPPEdit.setEnabled(true);
                mnuPP.setText("Production plan: " + plan.getName());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane,
                        e.getMessage(),
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
    }//GEN-LAST:event_mnuPPSaveAsActionPerformed

    private void mnuPauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPauseActionPerformed
        simulation.Pause();
    }//GEN-LAST:event_mnuPauseActionPerformed

    private void mnuStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuStopActionPerformed
        simulation.Stop();
        this.mnuPause.setEnabled(false);
        this.mnuStop.setEnabled(false);
        this.mnuResults.setEnabled(false);
    }//GEN-LAST:event_mnuStopActionPerformed

    private void mnuResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuResultsActionPerformed
        new FrmResults(simulation).setVisible(true);
    }//GEN-LAST:event_mnuResultsActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem mnuAbout;
    private javax.swing.JMenuItem mnuFM;
    private javax.swing.JMenuItem mnuFMEdit;
    private javax.swing.JMenuItem mnuFMNew;
    private javax.swing.JMenuItem mnuFMOpen;
    private javax.swing.JMenuItem mnuFMSave;
    private javax.swing.JMenuItem mnuFMSaveAs;
    private javax.swing.JMenu mnuFactoryModel;
    private javax.swing.JMenu mnuFile;
    private javax.swing.JMenu mnuHelp;
    private javax.swing.JMenuBar mnuMain;
    private javax.swing.JMenuItem mnuPP;
    private javax.swing.JMenuItem mnuPPEdit;
    private javax.swing.JMenuItem mnuPPNew;
    private javax.swing.JMenuItem mnuPPOpen;
    private javax.swing.JMenuItem mnuPPSave;
    private javax.swing.JMenuItem mnuPPSaveAs;
    private javax.swing.JMenuItem mnuPause;
    private javax.swing.JMenuItem mnuPlay;
    private javax.swing.JMenu mnuProductionPlan;
    private javax.swing.JMenuItem mnuQuit;
    private javax.swing.JMenuItem mnuResults;
    private javax.swing.JMenu mnuSimulation;
    private javax.swing.JMenuItem mnuStop;
    private javax.swing.JSeparator sepFM;
    private javax.swing.JSeparator sepFM1;
    private javax.swing.JSeparator sepFile;
    private javax.swing.JSeparator sepPP;
    private javax.swing.JSeparator sepPP1;
    private javax.swing.JSeparator sepSimulation;
    // End of variables declaration//GEN-END:variables

    public void reciveObject(Object object) {
        if (object instanceof FactoryModel) {
            factory = (FactoryModel) object;
            factory.setWhenModified(Calendar.getInstance());
            mnuProductionPlan.setEnabled(true);
        } else if (object instanceof ProductionPlan) {
            plan = (ProductionPlan) object;
            plan.setWhenModified(Calendar.getInstance());
            mnuPlay.setEnabled(true);
        } else if (object instanceof Simulation) {
            simulation = (Simulation) object;
            mnuResults.setEnabled(true);
            try {
                simulation.Start(factory, plan);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane,
                        e.getMessage(),
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

        }
    }
}
